package com.dm.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.dm.common.JsonResult;
import com.dm.entity.Room;
import com.dm.entity.Student;
import com.dm.service.IRoomService;
import com.dm.service.IStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dancun
 * @since 2021-05-02
 */
@RestController
@Api("寝室")
@Slf4j
@RequestMapping("/room")
public class RoomController {
    @Autowired
    IRoomService roomService;

    @Autowired
    IStudentService studentService;


    @PostMapping("/save")
    @ApiOperation("")
    public JsonResult save(@RequestBody Room room){
        try{
            roomService.save(room);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }
    @PostMapping("/delete")
    @ApiOperation("")
    public JsonResult delete(@RequestBody Room room){
        try{
            roomService.removeById(room.getId());
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }
    @PostMapping("/update")
    @ApiOperation("")
    public JsonResult update(@RequestBody Room room){
        try{
            roomService.updateById(room);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }

    @PostMapping("listrid")
    public JsonResult listRid(){
        List<Room> list = new ArrayList<>();
        try{
           QueryWrapper<Room> wrapper =Wrappers.query();
           wrapper.select("DISTINCT rid");
           list = roomService.list(wrapper);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(list);
    }

    @PostMapping("/list")
    @ApiOperation("")
    public JsonResult List(@RequestBody JSONObject jsonObject){
        Integer pageSize =0;
        Integer pageNum =0;
        Integer did = null;
        Integer rid = null;
        try{
            pageSize = Integer.parseInt(jsonObject.getString("pageSize"));
            pageNum = Integer.parseInt(jsonObject.getString("pageNum"));
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            try {
                did=Integer.parseInt(jsonObject.getString("did"));
            }catch (Exception e){
                e.printStackTrace();
                did = null;
            }
            rid = Integer.parseInt(jsonObject.getString("rid"));
        }catch (Exception e){
            rid = null;
            e.printStackTrace();
        }


        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=10;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;

        try{
            QueryWrapper<Room> wrapper = Wrappers.query();
            if(did!=null){
                wrapper.lambda().eq(Room::getDid,did);

            }
            if(rid!=null){
                wrapper.eq("r.rid",rid);

            }
            page = new PageInfo<Room>(roomService.listRoom(wrapper));
        }catch (Exception e){
           e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(page);
    }
    @PostMapping("/find")
    @ApiOperation("")
    public JsonResult find(@RequestBody Integer id){
        Room room = null;
        try{
           room = roomService.getOneRoom(id);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(room);
    }

    @PostMapping("cleanRoom")
    @ApiOperation("")
    public JsonResult cleanRoom(@RequestBody Map<String, List<Integer>> map){
        List<Integer> rooms = map.get("room");
        LambdaUpdateWrapper<Student> queryWrapper = Wrappers.lambdaUpdate();
        queryWrapper.in(Student::getRid,rooms);
        queryWrapper.set(Student::getRid,null);
        queryWrapper.set(Student::getBedid,null);
        try {
            studentService.update(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError("清空失败");
        }


        return JsonResult.renderSuccess("清空成功");
    }

    @PostMapping("getSutudent")
    @ApiOperation("")
    public JsonResult getSutudent(@RequestBody Integer id){
        List<Student> list = new ArrayList<>();
        QueryWrapper<Student> queryWrapper = Wrappers.query();
        queryWrapper.eq("rid",id);
        try{
            list = studentService.listStudent(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.renderError();
        }


        return JsonResult.renderSuccess(list);
    }

}
